package com.mxpio.erp.quality.controller;

import com.mxpio.erp.common.quality.entity.InspectionSchemeLine;
import com.mxpio.erp.common.quality.service.InspectionSchemeLineService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;
@Tag(name = "InspectionSchemeLineController", description = "质检方案行接口")
@RestController
@RequestMapping("/erp/quality/isline/")
public class InspectionSchemeLineController {

    @Autowired
    private InspectionSchemeLineService lineService;

    @GetMapping("list")
    @Operation(summary = "质检方案行列表", description = "获取质检方案行列表（无分页）", method = "GET")
    public Result<List<InspectionSchemeLine>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<InspectionSchemeLine> lines = lineService.list(InspectionSchemeLine.class, criteria);
        return Result.OK(lines);
    }

    @GetMapping("list/{code}")
    @Operation(summary = "根据code获取质检方案行列表", description = "根据code获取获取质检方案行列表（无分页）", method = "GET")
    public Result<List<InspectionSchemeLine>> listByBizNo(@Parameter(description="方案编号") @PathVariable(name = "code", required = true) String code) throws UnsupportedEncodingException {
        //Criteria c = Criteria.create().addCriterion("schemeCode", Operator.EQ, code);
        List<InspectionSchemeLine> lines = lineService.listBySchemeCode(code);
        return Result.OK(lines);
    }

    @GetMapping("page")
    @Operation(summary = "质检方案行列表", description = "获取质检方案行列表（分页）", method = "GET")
    public Result<Page<InspectionSchemeLine>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<InspectionSchemeLine> lines = lineService.listPage(InspectionSchemeLine.class, page, criteria);
        return Result.OK(lines);
    }


    @PostMapping("add")
    @Operation(summary = "新增质检方案行", description = "新增质检方案行", method = "POST")
    public Result<InspectionSchemeLine> add(@RequestBody InspectionSchemeLine inspectionSchemeLine) {
        lineService.save(inspectionSchemeLine);
        return Result.OK(inspectionSchemeLine);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑质检方案行", description = "编辑质检方案行", method = "PUT")
    public Result<InspectionSchemeLine> edit(@RequestBody InspectionSchemeLine inspectionSchemeLine) {
        lineService.update(inspectionSchemeLine);
        return Result.OK(inspectionSchemeLine);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除质检方案行", description = "删除质检方案行,多个id用英文逗号分隔", method = "DELETE")
    @Transactional
    public Result<InspectionSchemeLine> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            lineService.delete(InspectionSchemeLine.class, key);
        }
        return Result.OK();
    }
}
